package com.lizardmind.haofudoctor.ui.diagnosis.diagnose.userFill

import android.app.Activity
import android.arch.lifecycle.ViewModelProviders
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import com.linxiao.framework.activity.BaseActivity
import com.linxiao.framework.util.getDataBinding
import com.linxiao.framework.util.ke
import com.lizardmind.haofudoctor.R
import com.lizardmind.haofudoctor.databinding.ActivityFillDiagnoseOneBinding
import com.lizardmind.haofudoctor.other.DiagnoseDocumentsBean
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureConfig
import com.luck.picture.lib.config.PictureMimeType
import com.luck.picture.lib.entity.LocalMedia
import kotlinx.android.synthetic.main.include_title_text.*
import org.jetbrains.anko.sdk25.coroutines.onClick
import org.jetbrains.anko.startActivity
import java.lang.ref.WeakReference

/**
 * 用户填写问诊单的第一个界面
 * Created by Extends on 2017/11/10 10:35
 */
class FillDiagnoseOneActivity :BaseActivity() {
    private val binding by lazy { getDataBinding<ActivityFillDiagnoseOneBinding>(R.layout.activity_fill_diagnose_one) }
    override fun onCreatRootView(): View = binding.root

    override fun onInitView(savedInstanceState: Bundle?) {
        //初始化标题栏
        imgLeft.setImageResource(R.mipmap.back)
        imgLeft.onClick { finish() }
        titleText.text = "问诊单"

        val bean = intent.getSerializableExtra("bean") as DiagnoseDocumentsBean
        binding.apply {
            vm = ViewModelProviders.of(this@FillDiagnoseOneActivity).get(FillDiagnoseOneVM::class.java)
            vm!!.apply {
                activity = WeakReference(this@FillDiagnoseOneActivity)
                setBeanData(bean)
            }
            lifecycle.addObserver(vm!!)
            afdoSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
                override fun onNothingSelected(parent: AdapterView<*>?) {
                }

                override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                    vm!!.select.set(position)
                }
            }

            afdoS0.onClick { clickTongue(0) }
            afdoS1.onClick { clickTongue(0) }
            afdoY0.onClick { clickTongue(1) }
            afdoY1.onClick { clickTongue(1) }
            afdoY2.onClick { clickTongue(1) }

            afdoNext.onClick {
                startActivity<FillDiagnoseTwoActivity>("bean" to bean.documents)
            }
        }

    }

    fun clickTongue(type:Int){
        val temp = if(type==0){
            binding.vm!!.tongue_picture
        }else{
            binding.vm!!.related_picture
        }
        PictureSelector.create(this)
                .openGallery(PictureMimeType.ofImage())//全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()
                .selectionMode(PictureConfig.MULTIPLE)// 多选 or 单选 PictureConfig.MULTIPLE or PictureConfig.SINGLE
                .maxSelectNum(if(type==0) 2 else 3)
                .previewImage(true)// 是否可预览图片 true or false
                .isCamera(true)// 是否显示拍照按钮 true or false
                .compress(true)// 是否压缩 true or false
                .selectionMedia(temp)
                .minimumCompressSize(100)
                .cropCompressQuality(60)
                .forResult(PictureConfig.CHOOSE_REQUEST-type)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(resultCode== Activity.RESULT_OK){
            if(requestCode==PictureConfig.CHOOSE_REQUEST-0){
                val selectList = PictureSelector.obtainMultipleResult(data)
                binding.vm!!.tongue_picture.clear()
                binding.vm!!.tongue_picture.addAll(selectList)
            }else if(requestCode==PictureConfig.CHOOSE_REQUEST-1){
                val selectList = PictureSelector.obtainMultipleResult(data)
                binding.vm!!.related_picture.clear()
                binding.vm!!.related_picture.addAll(selectList)
            }
        }
    }
}